package ch.lue.tvsedit;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import ch.lue.tvsedit.model.Channel;
import ch.lue.tvsedit.model.ChannelGroup;

class ListRenderer extends JLabel implements ListCellRenderer {
	public ListRenderer() {
		setOpaque(true);
		setHorizontalAlignment(LEFT);
		setVerticalAlignment(CENTER);
	}

	/*
	 * This method finds the image and text corresponding to the selected value
	 * and returns the label, set up to display the text and image.
	 */
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		// Get the selected index. (The index param isn't
		// always valid, so just use the value.)
		//int selectedIndex = ((Integer) value).intValue();

		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}
		
		

		// Set the icon and text. If icon was null, say so.
		Object obj = list.getModel().getElementAt(index);
		
		if (obj instanceof ChannelGroup) {
			ChannelGroup group = (ChannelGroup) obj;
			
			setText(group.getDisplayName()+" - "+group.getSize());
		}
		
		if (obj instanceof Channel) {
			Channel chan = (Channel) obj;
			
			setText(chan.getName()+" - ");
		}

		return this;
	}
}